import Vue from 'vue'
import { login, getInfo, logout, getCode, getUserInfo } from '@/api/login'
import { ACCESS_TOKEN, ACCESS_TOKEN_Refresh, USER_ID, USER_NAME, COMPANY_ID, COMPANY_NAME, USER_AVATAR, PERMMISSION_LIST } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
import axios from 'axios'
const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {


    // 登录 缓存token
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        let info ={}
        info.mobile = userInfo.username
        info.password = userInfo.password
        login(info).then(response => {
          console.log("数据token",response.data)
          if (response.code === 200) {                       
            Vue.ls.set(ACCESS_TOKEN, response.data, 8 * 60 * 60 * 1000)
            Vue.ls.set(ACCESS_TOKEN_Refresh, response.data, 8 * 60 * 60 * 1000)
            console.log("数据token",response.data)
            commit('SET_TOKEN', response.data)
            resolve(response)
          } else if (response.code === 400) {
            //账号密码错误
            reject(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
  
      })
    },

    // 获取用户信息 权限
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        //后期优化
        // getUserInfo().then(res => {
        //   // commit('SET_NAME', { name: res.name, welcome: welcome() })
        //   // //暂无头像
        //   // // console.log('getUserInfo:' + JSON.stringify(res))
        //   // console.log("头像获取", res.data.avatar)
        //   // Vue.ls.set(USER_AVATAR, res.data.avatar, 24 * 60 * 60 * 1000)

        //   // Vue.ls.set(USER_ID, res.data.id, 24 * 60 * 60 * 1000)
        //   // Vue.ls.set(USER_NAME, res.data.staffName, 24 * 60 * 60 * 1000)
        //   // Vue.ls.set(COMPANY_NAME, res.data.company.name, 24 * 60 * 60 * 1000)
        //   // Vue.ls.set(COMPANY_ID, res.data.company.id, 24 * 60 * 60 * 1000)
        // }).catch(error => {
        //   reject(error)
        // })

        // // getPermissionByCom  getPermissionByUser   getInfo是本地的测试数据
        // getInfo().then(response => {
        //   const result = response.data
        //   // if (result.length > 0) {
        //   const role = {}
        //   role.permissions = result

        //   commit('SET_ROLES', 'admin')
        //   commit('SET_INFO', result)
        //   Vue.ls.set(PERMMISSION_LIST, result, 24 * 60 * 60 * 1000)

        //   // } else {
        //   // this.$message.error('暂无权限，请确认后再登入')
        //   // reject(new Error('getInfo: roles must be a non-null array !'))
        //   //windows
        //   // }
        //   resolve(response)

        // }).catch(error => {
        //   reject(error)
        // })

        Vue.ls.set(USER_ID, '123212131231', 24 * 60 * 60 * 1000)
        Vue.ls.set(USER_NAME, '123212131231', 24 * 60 * 60 * 1000)
        Vue.ls.set(COMPANY_NAME, '123212131231', 24 * 60 * 60 * 1000)
        Vue.ls.set(COMPANY_ID, '123212131231', 24 * 60 * 60 * 1000)
                  commit('SET_ROLES', 'admin')
          commit('SET_INFO', 'admin')
          resolve('')

      })
    },

    // 登出
    Logout({ commit, state }) {
      return new Promise((resolve) => {
        console.log("退出登陆111")
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        Vue.ls.remove(ACCESS_TOKEN)
        Vue.ls.remove(ACCESS_TOKEN_Refresh)
        Vue.ls.remove(USER_ID)
        Vue.ls.remove(USER_NAME)
        Vue.ls.remove(COMPANY_ID)
        Vue.ls.remove(COMPANY_NAME)
        resolve()
      })
    }

  }
}

export default user
